package com.Springdemo.controller;

import com.Springdemo.enity.Order;
import com.Springdemo.enity.User;
import com.Springdemo.service.OrderService;
import com.Springdemo.service.UserService;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lool
 * @Date: 2022-06-10 20:08
 * @Description:    1.购买  2.查询单笔订单 3.查询多笔订单
 */
@Controller
@RequestMapping("orders")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @GetMapping(value = {"orderid/{id}"})
    @ResponseBody
    public Order findById(@PathVariable("id") Integer id,HttpServletResponse response){
//查询单笔订单
        System.out.println("用户查询单笔订单成功");
        Order order=orderService.findById(id);
        System.out.println(order);
        return order;
    }

    @GetMapping(value = {"userid/{id}"})
    @ResponseBody
    public List<Order> findByIdList(@PathVariable("id") Integer id,HttpServletResponse response){
//查询多笔订单
        System.out.println("用户查询多边订单成功");
        List<Order> list=new ArrayList<Order>();
        list=orderService.findAllOrders(id);
        return list;
    }


//    @RequestMapping(value = {"buy"},method = RequestMethod.POST)
    @PostMapping(value = "/buy")
    @ResponseBody
    public int buyFruit(@RequestBody Order order,HttpServletResponse response){
        //购买
        System.out.println("购买成功");
        int counter=orderService.buyFruit(order);
        System.out.println("购买成功");
        return 200;

    }

}
